package main

import (
	"context"
	"fmt"
	"go-web-learning/src/05_grpc/3_multi_server/1_one_proto/proto_go/multi_server"
	"google.golang.org/grpc"
	"google.golang.org/grpc/grpclog"
	"net"
)

type VideoService struct {
}

func (s VideoService) Find(ctx context.Context, request *multi_server.VideoReq) (resp *multi_server.VideoResp, err error) {
	fmt.Println("----------video------------")
	fmt.Println(request)
	return &multi_server.VideoResp{Id: request.Id, Name: request.Name}, nil
}

type OrderServer struct {
}

func (o OrderServer) Order(ctx context.Context, request *multi_server.OrderReq) (resp *multi_server.OrderResp, err error) {
	fmt.Println("----------order------------")
	fmt.Println(request)
	return &multi_server.OrderResp{Id: request.Id, Name: request.Name}, nil
}

func main() {
	listen, err := net.Listen("tcp", ":8080")
	if err != nil {
		grpclog.Fatalf("Failed to listen: %s", err.Error())
	}
	defer listen.Close()
	s := grpc.NewServer()
	multi_server.RegisterVideoServiceServer(s, &VideoService{})
	multi_server.RegisterOrderServiceServer(s, &OrderServer{})

	err = s.Serve(listen)
	if err != nil {
		grpclog.Fatalf("Failed to register video server: %s", err.Error())
	}
	fmt.Println("server started on :8080")
}
